﻿using UnityEngine;
using System.Collections;

public class CameraMeteorScript : MonoBehaviour
{
    private VoxelTerrain voxelTerrain;

    [SerializeField]
    private int ExplosionRange = 5;

    void Start()
    {
        voxelTerrain = VoxelTerrain.Instance;
    }

    void Update()
    {
        RaycastHit hit;
        Ray screenRay = Camera.main.ScreenPointToRay(Input.mousePosition);

        if (Input.GetMouseButtonDown(0) && Physics.Raycast(screenRay, out hit))
        {
            Point3 explosionCenter = Point3.Round(hit.point);

            // Meteor right here
            for (int x = -ExplosionRange; x < ExplosionRange; x++)
                for (int y = -ExplosionRange; y < ExplosionRange; y++)
                    for (int z = -ExplosionRange; z < ExplosionRange; z++)
                    {
                        float offset = Random.Range(0.9f, 1.0f);

                        if (new Point3(x, y, z).sqrMagnitude * offset < ExplosionRange * ExplosionRange)
                        {
                            voxelTerrain.TryDestroyBlockAt(explosionCenter + new Point3(x, y, z));
                        }
                    }

            //MyCharacterController cc = GameObject.FindObjectOfType(typeof(MyCharacterController)) as MyCharacterController;

            //cc.AddMotion((cc.transform.position - explosionCenter).normalized * ExplosionRange - (cc.transform.position - explosionCenter));//(ExplosionRange - (cc.transform.position - explosionCenter).magnitude));
        }
    }
}
